<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { computed, PropType, reactive, ref, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysTaskDetailVo } from '@/api/system/task/SysTask/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
import form from '@/components/Form/src/Form.vue'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]

// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysTaskDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const cycleTypeRef = ref<string>('')
const { formRegister, submit, doWatch } = useBaseWriteView(props,undefined,currentRow => {
  cycleTypeRef.value=currentRow?.cycleType
})
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const cycleTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getCycleType = async () => {
  const res = await sysDictChooseListApi('sysTaskCycleType')
  cycleTypeOptions.value = res.data
}
getCycleType()
const executeStateOptions = ref<Array<KeyLabelValueVo>>([])
const getExecuteState = async () => {
  const res = await sysDictChooseListApi('sysTaskExecuteState')
  executeStateOptions.value = res.data
}
getExecuteState()
const platformTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getPlatformType = async () => {
  const res = await sysDictChooseListApi('platformType')
  platformTypeOptions.value = res.data
}
getPlatformType()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

// const formDataRef = ref(getFormData())
// const realFormData = ref({})
// watch(formDataRef, (newVal) => {
//   console.log('newVal', newVal)
// }, {
//   immediate: true,
//   deep: true
// })
//
// watch(
//     () => formDataRef,
//     (newVal) => {
//       console.log('newVal', newVal)
//       newVal.value.then((res) => {
//         realFormData.value = res
//       })
//     },
//     {
//       immediate: true,
//       deep: true
//     }
// )

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'name',
    label: '任务名',
    component: 'Input',
    componentProps: {
      placeholder: '请输入任务名',
      clearable: true
    }
  },
  {
    field: 'taskClass',
    label: '任务执行类',
    component: 'Input',
    componentProps: {
      placeholder: '请输入任务执行类',
      clearable: true
    }
  },
  {
    field: 'extraArg',
    label: '额外参数',
    component: 'Input',
    componentProps: {
      placeholder: '请输入额外参数',
      clearable: true
    }
  },
  {
    field: 'cycleType',
    label: '循环类型',
    value: '0',
    component: 'Select',
    componentProps: {
      placeholder: '请选择循环类型',
      clearable: true,
      filterable: true,
      options: cycleTypeOptions,
      onChange: (value) => {
        cycleTypeRef.value = value
      }
    }
  },
  {
    field: 'executeState',
    label: '执行状态',
    value: '0',
    component: 'Select',
    componentProps: {
      placeholder: '请选择执行状态',
      clearable: true,
      filterable: true,
      disabled: true,
      options: executeStateOptions
    }
  },
  {
    field: 'cron',
    label: 'cron表达式',
    component: 'Input',
    componentProps: {
      placeholder: '请输入cron表达式',
      clearable: true
    },
    hidden: computed(() => {
      return cycleTypeRef.value !== '20'
    })
  },
  {
    field: 'specifyTime',
    label: '指定执行时间',
    component: 'Input',
    componentProps: {
      placeholder: '请输入指定执行时间',
      clearable: true
    },
    hidden: computed(() => {
      return cycleTypeRef.value !== '0'
    })
  },
  {
    field: 'intervalTime',
    label: '间隔时间',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择间隔时间',
      clearable: true
    },
    hidden: computed(() => {
      return cycleTypeRef.value !== '10'
    })
  },
  {
    field: 'firstTime',
    label: '首次开始时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择首次开始时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    hidden: computed(() => {
      return cycleTypeRef.value !== '10'
    })
  },
  {
    field: 'firstDelayTime',
    label: '首次延迟时间',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择首次延迟时间',
      clearable: true
    },
    hidden: computed(() => {
      return cycleTypeRef.value !== '0'
    })
  },
  {
    field: 'enable',
    label: '是否启用',
    value: true,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否启用'
    }
  },
  {
    field: 'lastStartTime',
    label: '上次开始时间',
    component: 'DatePicker',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请选择上次开始时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      disabled: true
    }
  },
  {
    field: 'lastEndTime',
    label: '上次结束时间',
    component: 'DatePicker',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请选择上次结束时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      disabled: true
    }
  },
  {
    field: 'executeTimes',
    label: '执行次数',
    value: '0',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请输入执行次数',
      clearable: true,
      disabled: true
    }
  },
  {
    field: 'platformType',
    label: '平台类型',
    value: 'onflow-backend',
    component: 'Select',
    componentProps: {
      placeholder: '请选择平台类型',
      clearable: true,
      filterable: true,
      options: platformTypeOptions
    }
  },
  {
    field: 'remark',
    label: '备注',
    component: 'Input',
    componentProps: {
      placeholder: '请输入备注',
      clearable: true
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  },
  {
    field: 'createUserName',
    label: '创建人',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建人',
      disabled: true
    }
  },
  {
    field: 'updateUserName',
    label: '更新人',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新人',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  name: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 50) {
          callback(new Error('任务名不能超过50个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  taskClass: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('任务执行类不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  extraArg: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 1000) {
          callback(new Error('额外参数不能超过1000个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  cycleType: [required()],
  executeState: [required()],
  cron: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 64) {
          callback(new Error('cron表达式不能超过64个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  specifyTime: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 64) {
          callback(new Error('指定执行时间不能超过64个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  enable: [required()],
  platformType: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('平台类型不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  remark: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 500) {
          callback(new Error('备注不能超过500个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
